home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / scrtst / scrntest.bas < prev    next >
Encoding:
BASIC Source File  |  1995-01-24  |  16.3 KB  |  560 lines

  1. Option Explicit
  2. '
  3. ' User Defined Types
  4. '
  5.   '
  6.   ' Used for GetCursor - gets mouse location in screen coordinates.
  7.   '
  8.     Type POINTAPI
  9.       X As Integer
  10.       Y As Integer
  11.     End Type
  12.   '
  13.   ' Used by WM_SYSCOMMAND - converts mouse location.
  14.   '
  15.     Type ConvertPOINTAPI
  16.       xy As Long
  17.     End Type
  18.   '
  19.   ' .INI File Type - holds application .INI file information
  20.   '
  21.     Type INI_FILE_TYPE
  22.       fTop As Single
  23.       fLeft As Single
  24.       nStyle As Integer
  25.       lColor As Long
  26.       nGrab As Integer
  27.     End Type
  28.   '
  29.   ' Screen Size Type - holds screen size info
  30.   '
  31.     Type SCREEN_SIZE_TYPE
  32.       fVGA_HEIGHT As Single
  33.       fVGA_WIDTH As Single
  34.       fSVGA_HEIGHT As Single
  35.       fSVGA_WIDTH As Single
  36.       f1024_HEIGHT As Single
  37.       f1024_WIDTH As Single
  38.     End Type
  39.   '
  40.   ' Screen Rectangle type for API calls
  41.   '
  42.     Type lrect
  43.       Left As Integer
  44.       Top As Integer
  45.       Right As Integer
  46.       Bottom As Integer
  47.     End Type
  48. '
  49. ' API Calls
  50. '
  51.   '
  52.   ' Send Windows Message
  53.   '
  54.     Declare Function SendmessageByNum Lib "User" Alias "SendMessage" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As Integer
  55.   '
  56.   ' Get Cursor Position
  57.   '
  58.     Declare Sub GetCursorPos Lib "User" (lpPoint As POINTAPI)
  59.   '
  60.   ' Set Window Position
  61.   '
  62.     Declare Function SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal CX As Integer, ByVal CY As Integer, ByVal wFlags As Integer) As Integer
  63.   '
  64.   ' .INI File Functions
  65.   '
  66.     Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  67.     Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
  68.   '
  69.   ' Screen Capture Functions
  70.   '
  71.     Declare Function GetDesktopWindow Lib "User" () As Integer
  72.     Declare Function GetDC Lib "User" (ByVal hWnd As Integer) As Integer
  73.     Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal XSrc As Integer, ByVal YSrc As Integer, ByVal dwRop As Long) As Integer
  74.     Declare Function ReleaseDC Lib "User" (ByVal hWnd As Integer, ByVal hDC As Integer) As Integer
  75.     Declare Sub GetWindowRect Lib "User" (ByVal hWnd As Integer, lpRect As lrect)
  76.   '
  77.   ' System Menu API Declarations
  78.   '
  79.     Declare Function GetSystemMenu Lib "User" (ByVal hWnd As Integer, ByVal bRevert As Integer) As Integer
  80.     Declare Function RemoveMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
  81. '
  82. ' Module Constants
  83. '
  84.   '
  85.   ' For SetWindowPos API Call
  86.   '
  87.     Const SWP_NOMOVE = 2
  88.     Const SWP_NOSIZE = 1
  89.     Const Flags = SWP_NOMOVE Or SWP_NOSIZE
  90.     Const HWND_TOPMOST = -1
  91.     Const HWND_NOTOPMOST = -2
  92.     Const HWND_BOTTOM = 1
  93.     Const HWND_TOP = 0
  94.   '
  95.   ' .INI file constants
  96.   '
  97.     Const INI_FILENAME = "SCRNTEST.INI"
  98.     Const MAX_INI_STRING = 255
  99.   '
  100.   ' System Menu Constants
  101.   '
  102.     Const MF_SEPARATOR = &H800
  103.     Const MF_STRING = &H0
  104.     Const MF_ENABLED = 0
  105.     Const MF_BYCOMMAND = &H0
  106.     Const MF_UNCHECKED = &H0
  107.     Const MF_CHECKED = &H8
  108.     Const MF_BYPOSITION = &H400
  109.   '
  110.   ' Move Window Message
  111.   '
  112.     Const SC_MOVE = &HF010
  113. '
  114. ' Global Constants
  115. '
  116.   '
  117.   ' For Window Movement API calls
  118.   '
  119.     Global Const WM_LBUTTONUP = &H202
  120.     Global Const WM_SYSCOMMAND = &H112
  121.     Global Const MOUSE_MOVE = &HF012
  122.   '
  123.   ' Standard VB Keyboard Constants
  124.   '
  125.     Global Const ALT_MASK = 4
  126.     Global Const KEY_F4 = &H73
  127.     Global Const KEY_LBUTTON = &H1
  128.     Global Const KEY_RBUTTON = &H2
  129.     Global Const KEY_HOME = &H24
  130.     Global Const KEY_LEFT = &H25
  131.     Global Const KEY_UP = &H26
  132.     Global Const KEY_RIGHT = &H27
  133.     Global Const KEY_DOWN = &H28
  134.   '
  135.   ' Standard VB WindowState Constant
  136.   '
  137.     Global Const NORMAL = 0
  138.     Global Const MINIMIZED = 1
  139.   '
  140.   ' Constants for PlaceDialog Subroutine
  141.   '
  142.     Global Const DLG_STANDARD = 0
  143.     Global Const DLG_CENTERED = 1
  144.   '
  145.   ' MsgBox Warning message Constant
  146.   '
  147.     Global Const MB_ICONEXCLAMATION = 48
  148.   '
  149.   ' Form Show Constants
  150.   '
  151.     Global Const MODELESS = 0
  152.     Global Const MODAL = 1
  153.   '
  154.   ' Style Constants (numbers should match menu control arrray on frmUtility)
  155.   '
  156.     Global Const STYLE_VGA = 0
  157.     Global Const STYLE_SVGA = 1
  158.     Global Const STYLE_1024 = 2
  159. '
  160. ' Module Variables
  161. '
  162.   '
  163.   ' Throwaway Return variable
  164.   '
  165.     Dim r As Variant
  166.   '
  167.   ' INI variable
  168.   '
  169.     Dim muINIVals As INI_FILE_TYPE
  170.   '
  171.   ' Screen pixel/size type
  172.   '
  173.     Dim muScreenVals As SCREEN_SIZE_TYPE
  174.  
  175. Sub ExitProgram ()
  176.   '
  177.   ' Centralized Exit from program that saves .INI values
  178.   ' and makes sure that all forms are unloaded prior to
  179.   ' ending
  180.   '
  181.   Dim iLoop As Integer
  182.   SaveINIValues
  183.   For iLoop = Forms.Count - 1 To 0 Step -1
  184.     Unload Forms(iLoop)
  185.   Next
  186.   End
  187. End Sub
  188.  
  189. Sub GetINIValues ()
  190.   '
  191.   ' Gets INI values from File
  192.   ' (all Topic|Section and Default values are hard coded)
  193.   '
  194.   Dim nSize As Integer
  195.   Dim sReturnString As String
  196.   '
  197.   ' Get Form Top Value
  198.   '
  199.   sReturnString = String$(MAX_INI_STRING, 32)
  200.   nSize = GetPrivateProfileString("Screen Tester", "Top", "300", sReturnString, MAX_INI_STRING, INI_FILENAME)
  201.   muINIVals.fTop = Val(Mid$(sReturnString, 1, nSize))
  202.   '
  203.   ' Get Form Left Value
  204.   '
  205.   sReturnString = String$(MAX_INI_STRING, 32)
  206.   nSize = GetPrivateProfileString("Screen Tester", "Left", "300", sReturnString, MAX_INI_STRING, INI_FILENAME)
  207.   muINIVals.fLeft = Val(Mid$(sReturnString, 1, nSize))
  208.   '
  209.   ' Get Form Style Value
  210.   '
  211.   sReturnString = String$(MAX_INI_STRING, 32)
  212.   nSize = GetPrivateProfileString("Screen Tester", "Style", "0", sReturnString, MAX_INI_STRING, INI_FILENAME)
  213.   muINIVals.nStyle = Val(Mid$(sReturnString, 1, nSize))
  214.   '
  215.   ' Get Form Color Value
  216.   '
  217.   sReturnString = String$(MAX_INI_STRING, 32)
  218.   nSize = GetPrivateProfileString("Screen Tester", "Color", "0", sReturnString, MAX_INI_STRING, INI_FILENAME)
  219.   muINIVals.lColor = Val(Mid$(sReturnString, 1, nSize))
  220.   '
  221.   ' Get Screen Grab Destination Preference
  222.   '
  223.   sReturnString = String$(MAX_INI_STRING, 32)
  224.   nSize = GetPrivateProfileString("Screen Tester", "Grab", "0", sReturnString, MAX_INI_STRING, INI_FILENAME)
  225.   muINIVals.nGrab = Val(Mid$(sReturnString, 1, nSize))
  226. End Sub
  227.  
  228. Function GetScreenHeight () As Integer
  229.   '
  230.   ' Return Pixel Screen Height based on current style value
  231.   '
  232.   Select Case muINIVals.nStyle
  233.     Case STYLE_VGA
  234.       GetScreenHeight = 480
  235.     Case STYLE_SVGA
  236.       GetScreenHeight = 600
  237.     Case STYLE_1024
  238.       GetScreenHeight = 768
  239.   End Select
  240. End Function
  241.  
  242. Function GetScreenWidth () As Integer
  243.   '
  244.   ' Return Pixel Screen Width based on current style value
  245.   '
  246.   Select Case muINIVals.nStyle
  247.     Case STYLE_VGA
  248.       GetScreenWidth = 640
  249.     Case STYLE_SVGA
  250.       GetScreenWidth = 800
  251.     Case STYLE_1024
  252.       GetScreenWidth = 1024
  253.   End Select
  254. End Function
  255.  
  256. Sub GrabScreen ()
  257.   '
  258.   ' Captures Screen area where floating screen is located
  259.   ' and sends it to the Clipboard or to BMP File
  260.   '
  261.   ' This routine is based on information found in the
  262.   ' following Knowledge Base article:
  263.   '
  264.   '   How to Copy Entire Screen into a Picture Box in Visual Basic
  265.   '   Article ID: Q80670
  266.   '
  267.   Dim winSize As lrect
  268.   Dim hWndSrc As Integer
  269.   Dim hSrcDC As Integer
  270.   Dim XSrc As Integer
  271.   Dim YSrc As Integer
  272.   Dim nWidth As Integer
  273.   Dim nHeight As Integer
  274.   Dim hDestDC As Integer
  275.   Dim X As Integer
  276.   Dim Y As Integer
  277.   Dim dwRop As Long
  278.   '
  279.   ' Constant for Clipboard operations
  280.   '
  281.   Const CF_BITMAP = 2
  282.   '
  283.   ' Assign information of the source bitmap.
  284.   ' Note that BitBlt requires coordinates in pixels.
  285.   '
  286.   hWndSrc = GetDesktopWindow()
  287.   hSrcDC = GetDC(hWndSrc)
  288.   Call GetWindowRect(frmMain.hWnd, winSize)
  289.   XSrc = winSize.Left
  290.   YSrc = winSize.Top
  291.   nWidth = GetScreenWidth()
  292.   nHeight = GetScreenHeight()
  293.   '
  294.   ' Assign destination bitmap.
  295.   '
  296.   hDestDC = frmUtility.picCapture.hDC
  297.   X = 0
  298.   Y = 0
  299.   '
  300.   ' Set picture box to same size as screen being grabbed.
  301.   ' If picture box not the same size as picture being
  302.   ' BitBlt'ed to it, it will chop off all that does not
  303.   ' fit in the picture box.
  304.   '
  305.   frmUtility.picCapture.Top = 0
  306.   frmUtility.picCapture.Left = 0
  307.   frmUtility.picCapture.Width = (nWidth + 1) * screen.TwipsPerPixelX
  308.   frmUtility.picCapture.Height = (nHeight + 1) * screen.TwipsPerPixelY
  309.   '
  310.   ' Assign the value of the constant SRCOPYY to the Raster operation.
  311.   '
  312.   dwRop = &HCC0020
  313.   r = BitBlt(hDestDC, X, Y, nWidth, nHeight, hSrcDC, XSrc, YSrc, dwRop)
  314.   '
  315.   ' Release the DeskTopWindow's hDC to Windows.
  316.   ' Windows may hang if this is not done.
  317.   '
  318.   r = ReleaseDC(hWndSrc, hSrcDC)
  319.   If frmUtility!mnuPDDestOpt(0).Checked Then
  320.     '
  321.     ' Copy picture to Clipboard
  322.     '
  323.     Clipboard.SetData frmUtility.picCapture.Image, CF_BITMAP
  324.   ElseIf frmUtility!mnuPDDestOpt(1).Checked Then
  325.     '
  326.     ' Save to BMP File
  327.     '
  328.     SavePictureToFile
  329.   End If
  330.   '
  331.   ' Clear out picture box
  332.   '
  333.   frmUtility.picCapture.Picture = LoadPicture()
  334. End Sub
  335.  
  336. Sub InitScreenSizeValues ()
  337.   '
  338.   ' Initialize screen size variable type based on current screen.
  339.   '
  340.   muScreenVals.fVGA_HEIGHT = screen.TwipsPerPixelX * 480
  341.   muScreenVals.fVGA_WIDTH = screen.TwipsPerPixelY * 640
  342.   muScreenVals.fSVGA_HEIGHT = screen.TwipsPerPixelX * 600
  343.   muScreenVals.fSVGA_WIDTH = screen.TwipsPerPixelY * 800
  344.   muScreenVals.f1024_HEIGHT = screen.TwipsPerPixelX * 768
  345.   muScreenVals.f1024_WIDTH = screen.TwipsPerPixelY * 1024
  346. End Sub
  347.  
  348. Sub Main ()
  349.   '
  350.   ' Start of program
  351.   '
  352.   GetINIValues
  353.   InitScreenSizeValues
  354.   Load frmUtility
  355.   frmMain.Show
  356. End Sub
  357.  
  358. Sub MoveForm (frm As Form)
  359.   '
  360.   ' Issue keyboard Move command to a form
  361.   '
  362.   r = SendmessageByNum(frm.hWnd, WM_SYSCOMMAND, SC_MOVE, 0&)
  363. End Sub
  364.  
  365. Sub PlaceDialog (frmSource As Form, frmDialog As Form, iPos As Integer)
  366.   '
  367.   ' Place a dialog box (frmDialog) in specified relationship (iPos) to a source form (frmSource)
  368.   '
  369.   ' ****( Current Usage 1/10/95) ****
  370.   '
  371.   ' DLG_STANDARD = Offset dialog form 300 twips to right and 300 twips down from source form
  372.   ' DLG_CENTERED = Center dialog form in relation to source form.
  373.   '
  374.   ' If the display position would place the form outside the screen area, then set it
  375.   ' 60 twips from the edge of the screen.
  376.   '
  377.   '**************************************************************************************************************
  378.   Dim iLeft As Integer
  379.   Dim iTop As Integer
  380.   Select Case iPos
  381.     Case DLG_STANDARD
  382.       iLeft = frmSource.Left + 300
  383.       If iLeft < 0 Then
  384.   iLeft = 60
  385.       ElseIf (iLeft + frmDialog.Width) > screen.Width Then
  386.   iLeft = screen.Width - (frmDialog.Width + 60)
  387.       End If
  388.       iTop = frmSource.Top + 300
  389.       If iTop < 0 Then
  390.   iTop = 60
  391.       ElseIf iTop + frmDialog.Height > screen.Height Then
  392.   iTop = screen.Height - (frmDialog.Height + 60)
  393.       End If
  394.     Case DLG_CENTERED
  395.       iLeft = frmSource.Left + ((frmSource.Width / 2) - (frmDialog.Width / 2))
  396.       If iLeft < 0 Then
  397.   iLeft = 60
  398.       ElseIf (iLeft + frmDialog.Width) > screen.Width Then
  399.   iLeft = screen.Width - (frmDialog.Width + 60)
  400.       End If
  401.       iTop = frmSource.Top + ((frmSource.Height / 2) - (frmDialog.Height / 2))
  402.       If iTop < 0 Then
  403.   iTop = 60
  404.       ElseIf iTop + frmDialog.Height > screen.Height Then
  405.   iTop = screen.Height - (frmDialog.Height + 60)
  406.       End If
  407.     Case Else
  408.       iLeft = frmDialog.Left
  409.       iTop = frmDialog.Top
  410.       MsgBox "Programmer Error!" & Chr$(13) & Chr$(13) & "Invalid iPos sent to PlaceDialog", MB_ICONEXCLAMATION
  411.   End Select
  412.   frmDialog.Move iLeft, iTop
  413. End Sub
  414.  
  415. Sub SaveColor (lColor As Long)
  416.   '
  417.   ' Save color change in .INI type
  418.   '
  419.   muINIVals.lColor = lColor
  420. End Sub
  421.  
  422. Sub SaveGrab (nGrab As Integer)
  423.   '
  424.   ' Save Grab destination in .INI Type
  425.   '
  426.   muINIVals.nGrab = nGrab
  427. End Sub
  428.  
  429. Sub SaveINIValues ()
  430.   '
  431.   ' Saves .INI Values to file (note that Topic|Section values are hard coded)
  432.   '
  433.   r = WritePrivateProfileString("Screen Tester", "Top", CStr(muINIVals.fTop), INI_FILENAME)
  434.   r = WritePrivateProfileString("Screen Tester", "Left", CStr(muINIVals.fLeft), INI_FILENAME)
  435.   r = WritePrivateProfileString("Screen Tester", "Style", CStr(muINIVals.nStyle), INI_FILENAME)
  436.   r = WritePrivateProfileString("Screen Tester", "Color", CStr(muINIVals.lColor), INI_FILENAME)
  437.   r = WritePrivateProfileString("Screen Tester", "Grab", CStr(muINIVals.nGrab), INI_FILENAME)
  438. End Sub
  439.  
  440. Sub SavePictureToFile ()
  441.   '
  442.   ' Save Captured Image to a BMP File
  443.   '
  444.   On Error Resume Next
  445.  
  446.   '
  447.   ' File Open/Save Dialog Flag Constants
  448.   '
  449.   Const OFN_OVERWRITEPROMPT = &H2&
  450.   Const OFN_HIDEREADONLY = &H4&
  451.   Const OFN_PATHMUSTEXIST = &H800&
  452.   Const OFN_NOREADONLYRETURN = &H8000&
  453.  
  454.   frmUtility!dlgUtility.CancelError = True
  455.   frmUtility!dlgUtility.DefaultExt = ".BMP"
  456.   frmUtility!dlgUtility.DialogTitle = "Save Screen Image"
  457.   frmUtility!dlgUtility.Filename = ""
  458.   frmUtility!dlgUtility.Filter = "Bitmaps(*.bmp)|*.bmp"
  459.   frmUtility!dlgUtility.Flags = OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_NOREADONLYRETURN
  460.   frmUtility!dlgUtility.Action = 2
  461.   If Err = 0 Then
  462.     SavePicture frmUtility!picCapture.Image, frmUtility!dlgUtility.Filename
  463.     If Err <> 0 Then
  464.       MsgBox "The following error occured while attempting to save screen picture:" & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10) & Error$, MB_ICONEXCLAMATION, "File Save Error"
  465.     End If
  466.   End If
  467.   frmUtility!dlgUtility.CancelError = False
  468.   SendFormToBack frmMain
  469. End Sub
  470.  
  471. Sub SaveScreenPosition ()
  472.   '
  473.   ' Saves Screen Position prior to minimize or after a move.
  474.   '
  475.   muINIVals.fTop = frmMain.Top
  476.   muINIVals.fLeft = frmMain.Left
  477. End Sub
  478.  
  479. Sub SendFormToBack (frm As Form)
  480.   '
  481.   ' Places form at the lowest in the Zorder
  482.   '
  483.   r = SetWindowPos(frm.hWnd, HWND_BOTTOM, 0, 0, 0, 0, Flags)
  484. End Sub
  485.  
  486. Sub SetDestCheck (Index As Integer)
  487.   '
  488.   ' Toggle the Check mark on the screen capture
  489.   ' destination menu choices
  490.   '
  491.   Dim iLoop As Integer
  492.   For iLoop = 0 To 1
  493.     If iLoop = Index Then
  494.       frmUtility!mnuPDDestOpt(iLoop).Checked = True
  495.     Else
  496.       frmUtility!mnuPDDestOpt(iLoop).Checked = False
  497.     End If
  498.   Next
  499.   SaveGrab Index
  500. End Sub
  501.  
  502. Sub SetDialogMenu (frm As Form)
  503.   '
  504.   ' Removes menu items from the System menu of the specified Form (frm)
  505.   ' to achieve a standard dialog look.
  506.   '
  507.   ' ****> MaxButton and MinButton properties must be False and the form's borderstyle must be
  508.   ' ****> Fixed Double in order to achieve the correct effect.  This will
  509.   ' ****> remove all but the MOVE and CLOSE options from the system menu.
  510.   '
  511.   '**************************************************************************************************************
  512.   Dim hSysMenu As Integer
  513.   hSysMenu = GetSystemMenu(frm.hWnd, 0)
  514.   r = RemoveMenu(hSysMenu, 8, MF_BYPOSITION)
  515.   r = RemoveMenu(hSysMenu, 7, MF_BYPOSITION)
  516.   r = RemoveMenu(hSysMenu, 5, MF_BYPOSITION)
  517. End Sub
  518.  
  519. Sub SetStyle (frm As Form, nStyle As Integer)
  520.   '
  521.   ' Sets the style of the form (VGA, SVGA, 1024), checks the
  522.   ' popup menu and saves this new setting.
  523.   '
  524.   Dim iLoop As Integer
  525.   Select Case nStyle
  526.     Case STYLE_VGA
  527.       frm.Height = muScreenVals.fVGA_HEIGHT
  528.       frm.Width = muScreenVals.fVGA_WIDTH
  529.     Case STYLE_SVGA
  530.       frm.Height = muScreenVals.fSVGA_HEIGHT
  531.       frm.Width = muScreenVals.fSVGA_WIDTH
  532.     Case STYLE_1024
  533.       frm.Height = muScreenVals.f1024_HEIGHT
  534.       frm.Width = muScreenVals.f1024_WIDTH
  535.   End Select
  536.   For iLoop = 0 To 2
  537.     If iLoop = nStyle Then
  538.       frmUtility!mnuPType(iLoop).Checked = True
  539.     Else
  540.       frmUtility!mnuPType(iLoop).Checked = False
  541.     End If
  542.   Next
  543.   muINIVals.nStyle = nStyle
  544. End Sub
  545.  
  546. Sub SetUpForm (frm As Form, sID As String)
  547.   '
  548.   ' Sets form up based on .INI file info
  549.   '
  550.   Select Case sID
  551.     Case "Main"
  552.       frm.Move muINIVals.fLeft, muINIVals.fTop
  553.       frm.BackColor = muINIVals.lColor
  554.       SetStyle frm, muINIVals.nStyle
  555.     Case "Utility"
  556.       SetDestCheck muINIVals.nGrab
  557.   End Select
  558. End Sub
  559.  
  560.